#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Last Update:

'''docstring
'''

__revision__ = '0.1'
__author__ = 'lxd'
import urllib2, base64

class Javaeye(object):
    def __init__(self, name, pwd):
        self.name = name
        self.pwd = pwd

    def getAuth(self):
        #编码并去掉最后的换行符
        basic_auth = base64.encodestring('%s:%s' % (self.name, self.pwd))[:-1]
        return 'Basic '+basic_auth

    def fetch(self, request):
        auth = self.getAuth()
        agent = r'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.41 Safari/534.7'
        request.add_header('Authorization',auth)
        request.add_header('User-Agent',agent)

        opener = urllib2.build_opener()
        f = opener.open(request)
        return f.read()

    def login(self):
        """认证"""
        url = 'http://api.javaeye.com/api/auth/verify'
        request = urllib2.Request(url)
        return self.fetch(request)

    def write(self, words):
        """创建闲聊"""
        url = ' http://api.javaeye.com/api/twitters/create'
        data =  'body=%s&via=program' %words.encode('utf-8')
        request = urllib2.Request(url, data)
        return self.fetch(request)

if __name__ == '__main__':
    import config
    javaeye = Javaeye(config.javaeye_name, config.javaeye_pwd)
    r = javaeye.write(u'测试')
    if r:
        print 'javaeye ok'

